package com.example.understand_compose1.fet_retrofitusage.presentation

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.understand_compose1.R
import com.example.understand_compose1.fet_retrofitusage.data.api.IJsonPlaceholder
import com.example.understand_compose1.fet_retrofitusage.data.model.Post
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory


class FetRetrofitActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_fet_retrofit)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

//        val retrofit = Retrofit.Builder()
//            .baseUrl("https://api.github.com/")
//            .addConverterFactory(GsonConverterFactory.create())
//            .build()


    }

    private fun createPost() {
        val post: Post = Post(11011, "Title", "Body")
        val retrofitService = RetrofitClient.getInstance()
        val call = retrofitService.createPost(post)
        call.enqueue(object: Callback<Post> {
            override fun onResponse(call: Call<Post>, response: Response<Post>) {
                if (response.isSuccessful) {
                    // 处理成功的响应
                    val post = response.body()
                    // 例如，更新UI
                } else {
                    // 处理错误响应
                }
            }

            override fun onFailure(call: Call<Post>, t: Throwable) {
                // 处理请求失败
            }
        })
    }
}


object RetrofitClient {
    private const val BASE_URL = "https://jsonplaceholder.typicode.com/"

    fun getInstance(): IJsonPlaceholder {
        val retrofit = Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        return retrofit.create(IJsonPlaceholder::class.java)
    }
}
